import sys
from  PyQt5.QtWidgets import *
from PyQt5.QtGui import *
class TableDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUi()
    def initUi(self):
        self.setWindowTitle("QtableWidget案例")
        self.resize(400,300)
        conLayout=QHBoxLayout()
        tablewidget=QTableWidget()
        tablewidget.setRowCount(4)
        tablewidget.setColumnCount(3)
        conLayout.addWidget(tablewidget)
        tablewidget.setHorizontalHeaderLabels(['姓名','性别','年纪'])
        tablewidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        newItem=QTableWidgetItem("张三")
        newItem.setForeground(QBrush(QColor(255,0,0)))
        tablewidget.setItem(0,0,newItem)

        newItem = QTableWidgetItem("男")
        newItem.setForeground(QBrush(QColor(255,0,0)))

        tablewidget.setItem(0, 1, newItem)

        newItem = QTableWidgetItem("20")
        newItem.setForeground(QBrush(QColor(255,0,0)))

        tablewidget.setItem(0, 2, newItem)
        self.setLayout(conLayout)

if __name__ == '__main__':
    app=QApplication(sys.argv)
    table=TableDemo()
    table.show()
    sys.exit(app.exec_())